<template>
    <div>
        <van-address-edit :area-list="areaList" show-postal show-set-default show-search-result
            :search-result="searchResult" :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave"
            @change-area="changeArea" />
    </div>
</template>

<script>
import { areaList } from '@vant/area-data';
import { fetchAddUserAddress } from "../api/address.js"

export default {
    data() {
        return {
            areaList,
            searchResult: [],
            areaCode: '', // 记录省市区编码 格式： 110000-111200-111233
        };
    },
    methods: {
        async onSave(addressInfo) {
            let user_id = this.$store.state.userInfo.id;
            addressInfo.isDefault = addressInfo.isDefault ? 1 : 0;
            addressInfo.country = addressInfo.county;
            // console.log(222)
            // 父节点.appendChild(需要添加的节点,父节点.children[0])
            // console.log(addressInfo);return;
            let formData = {
                ...addressInfo,
                areaCode: this.areaCode,
            }
            // 调用接口，地址入库
            let result = await fetchAddUserAddress(user_id, formData)
            console.log(result)
            this.$router.back()
        },

        // 切换省市区会去切换
        changeArea(values) {
            this.areaCode = values.map(item => item.code).join('-')
        }

    },
};
</script>

<style lang="scss" scoped>
</style>
